package com.xiaoshuidi.cloud.module.iot.controller.admin.watermeter;

import com.alibaba.fastjson.JSONObject;
import com.xiaoshuidi.cloud.module.iot.annotation.OperationDataAccessAnnotation;
import com.xiaoshuidi.cloud.module.iot.common.ResultModel;
import com.xiaoshuidi.cloud.module.iot.dal.model.ammeter.AmmeterSwitchOnOffReqVO;
import com.xiaoshuidi.cloud.module.iot.dal.model.ammeter.AmmeterSwitchOnOffResVO;
import com.xiaoshuidi.cloud.module.iot.dal.model.watermeter.WaterMeterCommonRequestModel;
import com.xiaoshuidi.cloud.module.iot.api.device.vo.WaterMeterCommonResponseModel;
import com.xiaoshuidi.cloud.module.iot.dal.model.watermeter.WaterMeterSwitchOnOffReqVO;
import com.xiaoshuidi.cloud.module.iot.dal.model.watermeter.WaterMeterSwitchOnOffResVO;
import com.xiaoshuidi.cloud.module.iot.dal.model.watermeter.details.WaterMeterReportRequestModel;
import com.xiaoshuidi.cloud.module.iot.dal.model.watermeter.details.WaterMeterReportResponseModel;
import com.xiaoshuidi.cloud.module.iot.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.iot.framework.redislock.annoation.Lock4j;
import com.xiaoshuidi.cloud.module.iot.service.WaterMeterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 水表相关接口
 *
 * @author arvin
 * @date 2019-04-10 09:01
 * @description
 */
@Tag(name = "智能设备 - 智能水表")
@RestController
@RequestMapping(ApiConstants.NAME + "/watermeter")
public class WaterMeterController {
    private static final Logger LOGGER = LoggerFactory.getLogger(WaterMeterController.class);
    @Autowired
    private WaterMeterService waterMeterService;

    /**
     * 根据设备编号获取水表详情
     *
     * @return
     */
    @Operation(summary = "根据设备编号获取水表详情")
    @Parameter(name = "waterMeterCommonRequestModel", description = "水表请求实体")
    @PostMapping("/manage/getWatermeterInfo")
    public ResultModel<WaterMeterCommonResponseModel> getWatermeterInfo(@RequestBody WaterMeterCommonRequestModel waterMeterCommonRequestModel) {
        ResultModel<WaterMeterCommonResponseModel> resultModel = null;
        try {
            LOGGER.info("watermeter->获取水表详情,请求参数：{}", JSONObject.toJSONString(waterMeterCommonRequestModel));
            resultModel = this.waterMeterService.getWatermeterInfo(waterMeterCommonRequestModel);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("watermeter->获取水表详情异常,请求参数：{}，异常原因：{}", JSONObject.toJSONString(waterMeterCommonRequestModel), e.getMessage());
        }
        return resultModel;
    }

    /**
     * 刷新水表详情
     *
     * @param waterMeterCommonRequestModel
     * @return
     */
    @Operation(summary = "刷新水表详情")
    @Parameter(name = "waterMeterCommonRequestModel", description = "水表请求实体")
    @PostMapping("/manage/refreshWatermeterInfo")
    @Lock4j(keys = {"#waterMeterCommonRequestModel.deviceNo"}, expire = 60000, message = "请勿重复刷新")
    public ResultModel<WaterMeterCommonResponseModel> refreshWatermeterInfo(@RequestBody WaterMeterCommonRequestModel waterMeterCommonRequestModel) {
        ResultModel<WaterMeterCommonResponseModel> resultModel = null;
        try {
            LOGGER.info("watermeter->刷新水表详情,请求参数：{}", JSONObject.toJSONString(waterMeterCommonRequestModel));
            //刷新水表防重
            resultModel = this.waterMeterService.refreshWatermeterInfo(waterMeterCommonRequestModel);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("watermeter->刷新水表详情,请求参数：{}，异常原因：{}", JSONObject.toJSONString(waterMeterCommonRequestModel), e.getMessage());
            resultModel = ResultModel.createNativeFail(e.getMessage());
        }
        return resultModel;
    }

    /**
     * 水表用水明细
     *
     * @param waterMeterReportRequestModel
     * @return
     */
    @Operation(summary = "水表用水明细")
    @Parameter(name = "waterMeterCommonRequestModel", description = "水表请求实体")
    @PostMapping("/manage/getWatermeterReport")
    public ResultModel<WaterMeterReportResponseModel> getWatermeterReport(@RequestBody WaterMeterReportRequestModel waterMeterReportRequestModel) {
        ResultModel<WaterMeterReportResponseModel> resultModel = null;
        try {
            resultModel = this.waterMeterService.getWatermeterReport(waterMeterReportRequestModel);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("watermeter->水表用水明细,请求参数：{}，异常原因：{}", JSONObject.toJSONString(waterMeterReportRequestModel), e.getMessage());
            resultModel = ResultModel.createNativeFail(e.getMessage());
        }
        return resultModel;
    }

    @Operation(summary = "设置水表关阀停水")
    @Parameter(name = "waterMeterSwitchOnOffReqVO", description = "水表停水实体类")
    @OperationDataAccessAnnotation
    @PostMapping("/manage/setWaterMeterSwitchOff")
    public ResultModel<WaterMeterSwitchOnOffResVO> setWaterMeterSwitchOff(@RequestBody WaterMeterSwitchOnOffReqVO waterMeterSwitchOnOffReqVO) {
        ResultModel<WaterMeterSwitchOnOffResVO> resultModel = null;
        try {
            resultModel = this.waterMeterService.setWaterMeterSwitchOff(waterMeterSwitchOnOffReqVO);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("waterMeter->设置水表关阀停水,请求参数：{}，异常原因：{}", JSONObject.toJSONString(waterMeterSwitchOnOffReqVO), e.getMessage());
            resultModel = ResultModel.createNativeFail(e.getMessage());
        }
        return resultModel;
    }

    @Operation(summary = "设置水表开阀供水")
    @Parameter(name = "waterMeterSwitchOnOffReqVO", description = "水表供水实体类")
    @OperationDataAccessAnnotation
    @PostMapping("/manage/setWaterMeterSwitchOn")
    public ResultModel<WaterMeterSwitchOnOffResVO> setWaterMeterSwitchOn(@RequestBody WaterMeterSwitchOnOffReqVO waterMeterSwitchOnOffReqVO) {
        ResultModel<WaterMeterSwitchOnOffResVO> resultModel = null;
        try {
            resultModel = this.waterMeterService.setWaterMeterSwitchOn(waterMeterSwitchOnOffReqVO);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("waterMeter->设置水表开阀供水,请求参数：{}，异常原因：{}", JSONObject.toJSONString(waterMeterSwitchOnOffReqVO), e.getMessage());
            resultModel = ResultModel.createNativeFail(e.getMessage());
        }
        return resultModel;
    }


}
